package cn.tedu;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo08 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String passsword = scanner.nextLine();

        //获取连接
        try (Connection conn = DBUtils.getConn();){
            String sql = "select * from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            //执行查询sql语句
            ResultSet rs = ps.executeQuery();
            //判断是否查询到数据
            if (rs.next()){
                //查询到了
                //判断用户输入的和数据库查询到的正确密码是否一致,
                if (passsword.equals(rs.getString(1))){
                    System.out.println("登陆成功");
                }else{
                    System.out.println("密码错误");
                }
            }else{
                System.out.println("用户不存在!");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
